package com.sz.service.impl;

import com.sz.dao.OrderSettingDao;
import com.sz.exception.MyException;
import com.sz.pojo.OrderSetting;
import com.sz.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    @Transactional
    public void doImport(List<OrderSetting> orderSettingList) {
        //遍历List<OrderSetting>集合
        for (OrderSetting orderSetting : orderSettingList) {
            OrderSetting os = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
            //通过预约信息查询是否存在预约设置
            //存在
            if (os!=null){
                //判断已预约数是否大于最大预约数
                int reservations = os.getReservations();//已预约的人数
                int number = os.getNumber();//要更新的最大预约数
                //如果大于则报错
                if(reservations>number){
                    throw new MyException("已预约人数不能大于最大预约数");
                }
                //要是小于或等于则更新预约数
                orderSettingDao.updateNumber(orderSetting);

            }else{
                //不存在
                //添加记录
                orderSettingDao.add(orderSetting);
            }
        }


    }
}
